<div ng-include="sys_html('common/_header')"></div>
<style>
.cartmode{
	bottom: 2.75rem
}
.cartmode-backpage {bottom: 4.55rem}
.fui-number .minus:before, .fui-number .plus:before {
	border-left: 0 solid #d9d9d9;
}
.fui-number .minus:after, .fui-number .plus:after {
	border-right: 0 solid #d9d9d9;
}
.fui-number:before {
	border-top: 0 solid #d9d9d9;
}
.fui-number:after {border-top: 0 solid #d9d9d9;}
.fui-number .num {
	color:#666;
}
.fui-number .num {
	color:#666;
}
.fui-number.small {
	height: 1.2rem;
	width: 3.6rem;
	line-height: 1.2rem;
}
.fui-number .minus, .fui-number .plus {
	z-index:0;
}
.fui-number.small .minus{
	color: {{main.syscolor || '#63BE72'}};
	padding-top:1px;
	border: 1px solid {{main.syscolor || '#63BE72'}};
	border-radius:50%;
}
 .fui-number.small .plus {
	color: #fff;
	padding-top:1px;
	background: {{main.syscolor || '#63BE72'}};
	border: 1px solid {{main.syscolor || '#63BE72'}};
	border-radius:50%;
}
.jy2-headertop {position: absolute; min-height: 100%;padding-top:0.3rem;padding-bottom:0.3rem;margin-top:-2rem;border-top-right-radius:1rem;border-top-left-radius:1rem;}
.fui-header a.back:before {
	border-color: #fff
}
.fui-header:after {
	border-bottom: 0 solid #D9D9D9;
}
.fui-navbar .cartbtn {padding-right:0.3rem;background:#fff;opacity:1}
.fui-navbar .buybtn  {padding-left:0.3rem;background:#fff}
.fee-tag-nav, .fee-tagup-nav, .fee-disabled-nav {border-radius:1rem;height:2rem;line-height:1.95rem;color:#fff;width:100%;font-weight:400;font-size:0.7rem;margin:0.2rem 0;padding:0 0.2rem;text-align:center}
.fee-tagup {background:{{main.syscolor || 'red'}};color:#fff;width:auto;font-weight:400;font-size:0.55rem}
.fee-tag-nav ,.merchfee-tag-nav{background:{{main.syscolor || 'red'}};}
.merchfee-tag-nav, .merchfee-tagup-nav, .merchfee-disabled-nav {
    border-radius: 1rem;
    line-height: 1.5rem;
    color: #fff;
    width: 100%;
    font-weight: 400;
    font-size: 0.7rem;
    margin: 0.2rem 0;
    padding: 0 0.5rem;
    text-align: center;
}
.fee-tagup-nav, .merchfee-tagup-nav {background:{{main.syscolor || 'red'}};}
.fee-disabled-nav, .merchfee-disabled-nav {background:#bbb;color:#fff;}	
.coutag {padding:0.1rem 0.2rem;background:#ffedef;color:#db2e2a;font-size:0.55rem;border-radius: 0.2rem;}
input[type=checkbox],input[type=radio] {
	min-width: 0.9rem;
    min-height: 0.9rem;
    line-height: 0.9rem;
    margin-right: 0.5rem;
}
input[type="checkbox"]::before {
    border-radius: 100%;
}
input[type="checkbox"]:checked::before,input[type="radio"]:checked::before {
   border-radius: 100%;
}
</style>
<div class='fui-page  fui-page-current member-cart-page'>
	<div class="topbg" style="position: relative;text-align:center;width:100%;height:4.5rem;background: {{main.syscolor || '#7c1cc9'}};">
			<div class="fui-header" style="background:none">
				<div class="fui-header-left">
					<a class="back" style="color:#fff" href="javascript:" ng-click="goback()"></a>
				</div>
				<div class="title bold" style="font-size:1rem;color:#fff">我的购物车</div>
				<div class="fui-header-right" style="padding-right:0.5rem;color:#fff">
						<a class="btn-edit" style="color:#ddd" ng-click="toogle(pagedata,'show_edit')">{{ifelse(pagedata.show_edit,'完成','编辑')}}</a>
				</div>
			</div>
	</div>
	<div class="fui-content jy2-headertop cart-list" style="background:#f7f7f7">
		<div ng-if="pagedata.shopset.open_tips==1" class="tips tips-yellow">
			<i class="fa fa-bullhorn"></i> [<red>温馨提示</red>]：{{pagedata.shopset.tips}}
		</div>
		<div id="cart_container" style="padding-bottom:5rem">
		<div ng-if="pagedata.list.length<=0 && pagedata.loaded" class="content-empty">
			<img src="../addons/jy_weishop/images/nogoods.png" style="width: 6rem;margin-bottom: .5rem;"><br/><p style="color: #999;font-size: .75rem">您的购物车中没有商品哦！</p><br/>
			<a href="javascript:;" ng-click="access_json(main.menus[0].action || 'jy_weishop/index/')" class='btn btn-sm btn-danger-o external'style="border-radius: 100px;width:  7rem;font-size: .75rem">去首页逛逛吧</a>
		</div>
		<div class="radius" style="padding:0.2rem 0.5rem;">
		<div ng-repeat="(mk,item) in pagedata.list" class="fui-list-group" style="background:#fff;padding:0.3rem 0.5rem;border-radius:0.5rem">
			<div class="fui-list" style="align-items:flex-start;">
				<div class="fui-list-media" style="width:1rem;">
					<input type="checkbox" ng-checked="item.selected==1" ng-click="pagedata.toogle_mselect(mk)" name="checkbox" class="fui-radio fui-radio-danger" style="margin-top:0.2rem"/>
				</div>
				<div class="fui-list-inner">
							<div href="javascript:;" ng-click="access_json(item.url)" class="title" style="font-size:0.76rem">
								<span class="fui-tag fui-tag-danger" style="margin-right:0.2rem;background:linear-gradient(to right,rgba(244,221,187,0.6),rgba(235,216,174,1));color:#512f0a;width:auto;font-weight:400">{{item.typename}}</span>
								<span class="bold">{{item.merch.merchname}}</span>
							</div>
							<div class="subtitle">
								<span ng-if="item.merch.coupons.length>0" class="coutag" ng-click="access_json('jy_weishop/coupon.index//merchid='+item.merchid)">
								 领券:<span ng-repeat="c in item.merch.coupons" style="padding:0.1rem;">{{c.title}}<span ng-if="$index+1!=item.merch.coupons.length" style="padding-left:0.2rem">|</span></span>
								</span>
							</div>
				</div>
				<div class="angle">
				</div>
			</div>
			<div ng-if="item.store_isopen==1" class="fui-cellone" style="padding:0.5rem;padding-top:0">
						<div class="fui-cellone-icon" style="margin-right:0.3rem">
							<i class="icon icon-map" style="font-size:0.6rem"></i>
						</div>
						<div class="fui-cellone-info">
							<div>
							<span class="red">门店:</span>
							<span>{{item.store.title}}</span>
							<span style="color:{{main.syscolor || 'red'}}">{{item.store.pcd.city}}</span>
							</div>
							<i class="coutag icon icon-xinxi"> 如果商品没库存？切换门店试试!</i>
						</div>
						<div class="fui-cellone-remark" style="margin-right:0" href="javascript:;" ng-click="pagedata.showstores=true">
							切换
						</div>
			</div>
			<div ng-repeat="g in item.goods" class="fui-list goods-item align-start">
				<div class="fui-list-media " ng-show="!pagedata.show_edit">
					<input ng-if="g.canbuy && !g.outtype" type="checkbox" ng-checked="g.selected==1" ng-click="pagedata.toogle_select(mk,$index)" name="checkbox" class="fui-radio fui-radio-danger cartmode check-item choose_goods"/>
					<input ng-if="!g.canbuy || g.outtype" type="checkbox" disabled="disabled" class="fui-radio fui-radio-danger cartmode check-item"/>
				</div>
				<div class="fui-list-media" ng-show="pagedata.show_edit">
				   <input type="checkbox" name="checkbox" ng-checked="pagedata.del.indexOf(';'+g.id+';')>=0" ng-click="duoxuans(g.id,pagedata,'del')" class="fui-radio fui-radio-danger edit-item del_history"/>
				</div>

				<div class="fui-list-media image-media">
					<a href="javascript:;" ng-click="pagedata.getgoodsdetail(g.goodsid);">
						<img ng-src="{{tomedia(g.image) || '../core/resource/images/nopic.jpg'}}" class="">
					</a>
				</div>
				<div class="fui-list-inner">
					<a href="javascript:;" ng-click="pagedata.getgoodsdetail(g.goodsid);">
						<div class="subtitle">
							<span ng-if="g.type==4" class='fui-label fui-label-danger'>批发</span>
							<span ng-if="g.discounttype>0 && g.isnodiscount ==0" class='fui-label fui-label-danger'>折扣</span>
							<div ng-if="g.seckillprice>0" class="fui fui-label fui-label-danger">{{g.seckilltag}}</div>
							{{g.title}}
						</div>
					</a>
						<div class="subtitle cart-option" style="line-height:1">
							<div class="choose-option">{{g.optiontitle ||'默认'}}</div>
							<span class="num bold">x {{g.total}} /{{g.unit || '件'}} <span ng-if="g.weight && g.weight!='0.00'">（{{g.weight}}g）</span></span>
						</div>
						<div class="subtitle" ng-if="g.canbuy && g.outtype" style="color:red;font-size:0.55rem;font-weight:400;">
								{{g.outnumtitle}}
						</div>
					<div ng-if="g.active && g.active.title" class="red" style="line-height:1">
							{{g.active.title}}
					</div>
					<div class='price'>
						<span class="bigprice text-danger">￥<span class='marketprice'>{{g.active.price || g.price}}</span></span>
						<div ng-if="g.canbuy" class="fui-number small">
								<div class="minus" ng-click="pagedata.cartminus(mk,$index)"><i class="icon icon-subtract" style="font-size:0.7rem"></i></div>
								<input class="num shownum" type="number" ng-model="g.total" style="width:50px" ng-change="pagedata.refresh();pagedata.upcart(g.id,g.total)"/>
								<div ng-if="g.kucun>=g.total" class="plus" ng-click="pagedata.cartplus(mk,$index)"><i class="icon icon-tianjia" style="font-size:0.7rem"></i></div>
								<div ng-if="g.kucun<g.total" class="plus" style="color:#ddd"><i class="icon icon-tianjia" style="font-size:0.7rem"></i></div>
						</div>
						<div ng-if="!g.canbuy" class="bold red">
							{{g.nobuytitle}}
						</div>
					</div>
					</div>
				</div>
				<div ng-if="pagedata.list.length!=1" class="fui-cell-group" style="margin-top:0">
					<div class="fui-cell">
						 <div class="fui-cell-label" style='width:auto'>合计:<span class="bigprice red" style="font-weight:600"> ￥<span style="font-size:0.8rem">{{item.totalprice}}</span></span></div>
						  <div class="fui-cell-info"></div>
						 <div class="fui-cell-remark noremark bold">
						 <div ng-click="pagedata.check_cartxieyi(item.merchid)" ng-class="{'merchfee-disabled-nav':item.total<=0,'merchfee-tag-nav':item.total>0}">
								去结算
						</div>
						 </div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div id="footer_container">
	<div ng-if="pagedata.list.length==1 && !pagedata.show_edit" ng-class="{'fui-footer cartmode':1, 'cartmode-backpage':pagedata.is_backpage && main.openbackpage==1}">
		<div class="fui-list noclick">
			<div class="fui-list-media">
				<label class="checkbox-inline checkall" style="margin-right:0">
					<input type="checkbox" ng-checked="pagedata.isselectall==1" name="checkbox" class="fui-radio fui-radio-danger" style="margin-right:0.3rem" ng-click="pagedata.selectall();"/>&nbsp;全选
				</label>
			</div>
			<div class="fui-list-inner">
				<div class='subtitle l' style="font-size:0.7rem;padding-bottom:0.15rem">合计:<span class="bigprice" style="font-weight:600"> ￥</span><span class='totalprice  bigprice' style="font-weight:600;">{{pagedata.totalprice}}</span></div>
			</div>
			<div class='fui-list-angle' style="padding-left:2px;padding-right:0.5rem">
				<div ng-click="pagedata.check_cartxieyi()" ng-class="{'fee-disabled-nav':pagedata.total<=0,'fee-tag-nav':pagedata.total>0}">
						结算(<span class='total'>{{pagedata.total}}</span>)
				</div>
			</div>
		</div>
	</div>
	<div ng-class="{'fui-footer cartmode':1, 'cartmode-backpage':pagedata.is_backpage && main.openbackpage==1}" ng-show="pagedata.show_edit">
		<div class="fui-list noclick">
			<div class="fui-list-media">
				<label class="checkbox-inline editcheckall"><input type="checkbox" name="checkbox" class="fui-radio fui-radio-danger " onclick="var ck = this.checked;$('.del_history').each(function(){if(this.checked != ck){$(this).click();}});" />&nbsp;全选</label>
			</div>
			<div class='fui-list-angle'>				
				<div class="fee-tag-nav" ng-click="post({},'del','del='+pagedata.del)">删除</div>
			</div>
		</div>
	</div>
	</div>
</div>
<div ng-include="sys_html('shop/_goodsdetail')"></div>
<div ng-include="sys_html('shop/_selectstores')"></div>
</div>
<div ng-include="sys_html('common/_menu')"></div>
<div ng-include="sys_html('common/_footer')"></div>
<div ng-include="sys_html('shop/_cartxieyi')"></div>
<script>
scope.pagedata.isfromcart=1;
scope.pagedata.list=[];
scope.pagedata.get_list=function(){
	scope.tool.get_list('jy_weishop/member.cart/getlist/ismerch=1',true,function(res){
		scope.pagedata.refresh();
	});
}
//首次加载
scope.pagedata.get_list(true);
//滑到页面底部时加载下一页
scope.pagedata.scroll_bottom=function(){
	scope.tool.get_list('jy_weishop/member.cart/getlist/ismerch=1');
}
//减少
scope.pagedata.cartminus=function(mk,index){
	var cart=scope.pagedata.list[mk]['goods'][index]
	scope.post_json('jy_weishop/member.cart/minus/',{'id':cart.id},function(){
				if(cart.total==1){
					scope.pagedata.list[mk]['goods'].splice(index,1);
				}
				cart.total--;
				if(cart.outtype==1 && cart.total + cart.order_goodscount<=cart.usermaxbuy){
					cart.outtype=false;
				}
				if(cart.outtype==2 && cart.total<=cart.kucun){
					cart.outtype=false;
				}
				if(cart.outtype==4 && cart.total<=cart.maxbuy){
					cart.outtype=false;
				}
				if(cart.outtype==5 && cart.total<=cart.active.maxnum){
					cart.outtype=false;
				}
				scope.pagedata.cart=cart;
				scope.pagedata.refresh();
				if(cart.total==0){
					scope.message('删除成功！');
				}else{
					scope.message('减少成功！');
				}
				scope.$evalAsync();
	})
}
//增加
scope.pagedata.cartplus=function(mk,index){
	var cart=scope.pagedata.list[mk]['goods'][index]
	if(cart.kucun<=cart.total){
		scope.message('超库存，不能再加了！');
		return false;
	}
	scope.post_json('jy_weishop/member.cart/plus/',{'id':cart.id},function(res){
			if(res=='success'){
				cart.total++;
				if(cart.outtype==3 && cart.total>=cart.minbuynum){
					cart.outtype=false;
				}
				scope.message('添加成功！');
				scope.pagedata.refresh();
				scope.$evalAsync();
			}else{
				scope.message('添加失败:'+res);
			}
	});
}
//选择
scope.pagedata.toogle_mselect=function(mk){
	var merch=scope.pagedata.list[mk];
	var selected=merch.selected==1?0:1;
	merch.selected=selected;
	console.log(merch);
	scope.post_json('jy_weishop/member.cart/selectmerch/isnew=1',{'id':merch.merchid,'selected':selected},function(res){
		for(var g=0;g<merch.goods.length;g++){
			merch.goods[g].selected=selected
		}
		scope.pagedata.list[mk]=merch;
		scope.pagedata.refresh();
		scope.$evalAsync();
	})
}
//选择
scope.pagedata.toogle_select=function(mk,index){
	var cart=scope.pagedata.list[mk]['goods'][index];
	var selected=cart.selected==1?0:1;
	scope.post_json('jy_weishop/member.cart/select/',{'id':cart.id,'selected':selected},function(res){
		if(res=='success'){
			cart.selected=selected;
			scope.pagedata.list[mk]['goods'][index]=cart;
			scope.pagedata.refresh();
			scope.$evalAsync();
		}
	})
}
//选择
scope.pagedata.postselectall=function(type){
	scope.post_json('jy_weishop/member.cart/selectall/type='+type,{},function(res){	
	})
	scope.pagedata.refresh();
	scope.$evalAsync();
}
//刷新总数和总价
scope.pagedata.refresh=function(){
	var total=0,totalprice=0.00;
	scope.pagedata.isselectall=1;
	for(var i=0;i<scope.pagedata.list.length;i++){
	   var merch=scope.pagedata.list[i];
	   merch.total=0;
	   merch.totalprice=0.00;
	   angular.forEach(merch.goods,function(cart){
			if(cart.selected==1){
				cart.total=parseInt(cart.total);
				merch.total+=cart.total;
				merch.totalprice+=cart.total*cart.price;
				if(merch.selected==1){
					total+=cart.total;
					totalprice+=cart.total*cart.price;
				}
			}else{
				if(cart.canbuy){
					scope.pagedata.isselectall=0;
				}
			}
		});
		merch.totalprice=scope.price(merch.totalprice);
		scope.pagedata.list[i]=merch;
	}
	scope.pagedata.total=total;
	scope.pagedata.totalprice=scope.price(totalprice);
}
//刷新总数和总价
scope.pagedata.selectall=function(){
	if(scope.pagedata.isselectall==1){
		for(var i=0;i<scope.pagedata.list.length;i++){
		   var merch=scope.pagedata.list[i];
		   for(var g=0;g<merch.goods.length;g++){
				merch.goods[g].selected=0
			}
			merch.selected=0;
			scope.pagedata.list[i]=merch;
		}
		scope.pagedata.postselectall(0);
	}else{
		for(var i=0;i<scope.pagedata.list.length;i++){
		   var merch=scope.pagedata.list[i];
		    for(var g=0;g<merch.goods.length;g++){
				if(merch.goods[g].canbuy){
					merch.goods[g].selected=1;
				}
			}
			merch.selected=1;
			scope.pagedata.list[i]=merch;
		}
		scope.pagedata.postselectall(1);
	}
}
scope.pagedata.upcart=function(id,total){
	scope.pagedata.upgoodscart=[];
	scope.pagedata.upgoodscart[id]=total;
	if(scope.pagedata.upgoodscart && total && id){
		scope.post_json('jy_weishop/member.cart/upcart/',scope.pagedata.upgoodscart,function(res){
		});
	}
}
scope.pagedata.check_cartxieyi=function(merchid){
	scope.pagedata.show_merchid=merchid;
	if(scope.pagedata.shopset.open_cartxieyi==1){
		scope.pagedata.show_cartxieyi=true;
		scope.$evalAsync();
	}else{
		scope.pagedata.check_buy();
	}
}
scope.pagedata.check_buy=function(merchid){
	var merchid=scope.pagedata.show_merchid;
	if(scope.pagedata.list.length==1 && !merchid && scope.pagedata.list[0]){
		merchid=scope.pagedata.list[0].merchid;
	}
	merchid=merchid?merchid:0;
	scope.get('jy_weishop/member.cart/check_buy/isresurl=1&merchid='+merchid,function(resurl){
		scope.access_json(resurl);
	});
}
scope.pagedata.getstores=function(lat_lng){
	scope.post_json('/get_data/getstores/',{'lat_lng':lat_lng},function(res){
			scope.pagedata.stores =res.stores
			scope.pagedata.stores_count =res.stores_count
			scope.$evalAsync();
	});
}
if(!scope.pagedata.lat_lng && !scope.main.lat_lng){
	window.scope.location(function(res){
		scope.main.lat_lng=res.lat_lng;
		scope.main.pcd=res.addressComponents;
		scope.pagedata.getstores(res.lat_lng);
	},scope.pagedata.setting.no_location);
}
scope.pagedata.selectstores=function(item){
	console.log('选择门店'+item.id);
	scope.pagedata.params.store_id=item.id;
	scope.pagedata.get_list(true);
}
</script>
